package com.liang.service.impl;



import com.liang.empty.RoleAuthorityMenu;
import com.liang.mapper.AuthorityDAO;
import com.liang.pojo.Authority;
import com.liang.service.AuthorityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *@作者:lsk
 *Service实现类
 *@Date:2023/07/13
*/
@Service
public class AuthorityServiceImpl implements AuthorityService {


	@Resource
	private AuthorityDAO authoritydao;


	/**
	 * 删除角色管理权限
	 */
	@Override
	public Integer deleteAuthority(Map map) {
		return authoritydao.deleteAuthority(map);
	}

	/**
	 * 连表查询角色的权限
	 */
	@Override
	public List<RoleAuthorityMenu> selectRoleAuthority(String roleId) {
		List<RoleAuthorityMenu> listx = authoritydao.selectRoleAuthorityOnlyMy(roleId);
		List<RoleAuthorityMenu> menusList = listx.stream().filter(menu ->
				menu.getParentId() ==0
		).map((menu) ->{
			menu.setRoleAuthorityMenuList(getChildCategory(menu,listx));
			return menu;
		}).collect(Collectors.toList());
		return menusList;
	}

	/**
	 * 连表查询角色权限包括有和没有
	 */
	@Override
	public List<RoleAuthorityMenu> selectRoleAuthorityAll(String roleId) {
		List<RoleAuthorityMenu> listx = authoritydao.selectRoleAuthority(roleId);
		List<RoleAuthorityMenu> menusList = listx.stream().filter(menu ->
				menu.getParentId() ==0
		).map((menu) ->{
			menu.setRoleAuthorityMenuList(getChildCategory(menu,listx));
			return menu;
		}).collect(Collectors.toList());
		return menusList;
	}


	/**
	 * 连表查询角色的菜单按钮权限
	 */
	@Override
	public List<RoleAuthorityMenu> selectRoleAuthorityButton(Map<String, Object> param) {
		return authoritydao.selectRoleAuthorityButton(param);
	}

	private List<RoleAuthorityMenu> getChildCategory(RoleAuthorityMenu father,List<RoleAuthorityMenu> all) {
		List<RoleAuthorityMenu> child = all.stream().filter(menu -> {
			return menu.getParentId().equals(father.getMenuId());
		}).map(menu -> {
			//递归找子菜单
			menu.setRoleAuthorityMenuList(getChildCategory(menu, all));
			return menu;
		}).collect(Collectors.toList());
		return child;
	}

	/**
	 insert:(插入)
	*/
	public Object insert(Authority demo){
		authoritydao.insert(demo);
		return demo;
	}



	/**
	 insertOrUpdate(插入或更新(根据主键或唯一索引更新，主键和唯一索引不会改变))
	*/
	public Integer insertOrUpdate(Authority demo){
		return authoritydao.insertOrUpdate(demo);
	}



	/**
	 insertBath:(批量插入)
	*/
	public Integer insertBatch(List<Authority> list){
		return authoritydao.insertBatch(list);
	}



	/**
	 insertOrUpdateBatch:(批量插入或更新)
	*/
	public Integer insertOrUpdateBatch(List<Authority> list){
		return authoritydao.insertOrUpdateBatch(list);
	}



	/**
	 selectList:(根据参数查询集合)
	*/
	public List<Authority> selectList(Map<String,Object> param){
		if(param.get("pageNum") != null){
			int start = Integer.parseInt(param.get("pageSize").toString()) * (Integer.parseInt(param.get("pageNum").toString()) - 1);
			param.put("pageNum",start);
		}
		return authoritydao.selectList(param);
	}



	/**
	 selectCount:(根据集合查询数量)
	*/
	public Long selectCount(Map<String,Object> param){
		return authoritydao.selectCount(param);
	}



}